package com.beordie.controller;

import com.beordie.model.*;
import com.beordie.mvc.ResponseText;
import com.beordie.service.INoticeService;
import com.beordie.service.IPunishmentService;
import com.beordie.service.impl.NoticeServiceImpl;
import com.beordie.service.impl.PunishmentServiceImpl;
import com.beordie.utils.DateFormatUtils;
import com.beordie.utils.JsonUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Time;
import java.util.Date;
import java.util.List;

/**
 * @Classname
 * @Description
 * @Date 2021/7/11 21:24
 * @Created 30500
 */
public class NoticeController {
    INoticeService noticeService = new NoticeServiceImpl();
    IPunishmentService punishmentService = new PunishmentServiceImpl();

    @ResponseText("/notice/getAll.udo")
    public String getAllCars(HttpServletRequest request, HttpServletResponse response) {
        return null;
    }

    @ResponseText("/notice/insert.udo")
    public String insert(HttpServletRequest request, HttpServletResponse response) {
        String number = request.getParameter("number");
        String date = request.getParameter("date");
        String time = request.getParameter("time");
        String location = request.getParameter("location");
        String record = request.getParameter("record");
        String driver = request.getParameter("driver");
        String police = request.getParameter("police");
        String car = request.getParameter("car");
        String punishment = request.getParameter("punishment");
        Notice notice = new Notice(0, number, DateFormatUtils.getDate(date), DateFormatUtils.getTime(time), location, record);

        String[] calculate = punishment.split(" ");
        for (String string : calculate) {
            System.out.println(string);
        }
        int result = noticeService.insert(notice, driver, car, police, calculate);
        Message message = new Message();
        if (result  == 1) {
            message.setStatus(0);
            message.setResult("新增成功");
        } else {
            message.setStatus(-1);
            message.setResult("新增失败");
        }
        return JsonUtils.parseObject(message);
    }

    @ResponseText("/notice/delete.udo")
    public String deleteById(HttpServletRequest request, HttpServletResponse response) {
        int id = Integer.parseInt(request.getParameter("id"));
        int result = noticeService.deleteById(id);

        Message message = new Message();
        if (result  > 0) {
            message.setStatus(0);
            message.setResult("删除成功");
        } else {
            message.setStatus(-1);
            message.setResult("删除失败");
        }
        return JsonUtils.parseObject(message);
    }

    @ResponseText("/notice/update.udo")
    public String updateById(HttpServletRequest request, HttpServletResponse response) {
        String date = request.getParameter("date");
        String time = request.getParameter("time");
        String location = request.getParameter("location");
        String record = request.getParameter("record");
        String driver = request.getParameter("driver");
        String police = request.getParameter("police");
        String car = request.getParameter("car");
        String punishment = request.getParameter("punishment");
        int id = Integer.parseInt(request.getParameter("id"));

        String[] calculate = punishment.split(" ");
        for (String string : calculate) {
            System.out.println("前端数据得到接收  " + string);
        }
        Notice notice = new Notice(0, "", DateFormatUtils.getDate(date), DateFormatUtils.getTime(time), location, record);

        int result = noticeService.updateById(notice, driver, car, police, calculate ,id);
        Message message = new Message();
        if (result  == 1) {
            message.setStatus(0);
            message.setResult("更新成功");
        } else {
            message.setStatus(-1);
            message.setResult("更新失败");
        }
        return JsonUtils.parseObject(message);
    }

    @ResponseText("/notice/find.udo")
    public String findByKey(HttpServletRequest request, HttpServletResponse response) {
        String number = request.getParameter("key");
        String way = request.getParameter("way");
        System.out.println(way);
        FormatNotice result = noticeService.getByKey(number, way);

        Message message = new Message();
        if (result != null) {
            message.setStatus(0);
            message.setResult("查询成功");
            message.setData(result);
        } else {
            message.setStatus(-1);
            message.setResult("查询失败");
        }
        return JsonUtils.parseObject(message);
    }

    @ResponseText("/notice/findByNumber.udo")
    public String findByNumber(HttpServletRequest request, HttpServletResponse response) {
        String number = request.getParameter("key");
        List<Punishment> punishments = null;
        Message message = new Message();
        FormatNotice notice = noticeService.findByNumber(number);
        if (notice != null) {
            punishments = punishmentService.getAllByNotice(notice.getId());
            if (punishments != null && punishments.size() > 0) {
                message.setStatus(0);
                message.setResult("查询成功");
                message.setData(punishments);
            } else {
                message.setStatus(-1);
                message.setResult("查询失败");
            }
        } else {
            message.setStatus(-1);
            message.setResult("查询失败");
        }
        return JsonUtils.parseObject(message);
    }
}
